﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Register Src="../Shared/Header.ascx" TagName="Header" TagPrefix="uc1" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>数据字典管理</title>
    <uc1:Header ID="Header1" runat="server" />
    <script type="text/javascript" language="javascript">
        $(function () {

            $('#grid').treegrid({
//                title: '数据字典分类信息',
                iconCls: 'icon-ok',
                nowrap: false,                      //数据是否显示在一行里
                striped: true,                      //奇偶行使用不同背景色
                url: '/DicCategory/LoadAll/',       //从远程站点请求数据的 URL
                fitColumns: true,                   //适应表格的宽度并且防止水平滚动
                fit: true,
                idField: 'id',                      //标识字段
                treeField: 'name',                  //定义树节点的字段
                sortName: 'index',                  //定义可以排序的列
                sortOrder: 'asc',                   //定义列的排序顺序，只能用 'asc' 或 'desc'
                remoteSort: false,                  //定义是否从服务器给数据排序
                singleSelect: true,                //是否允许选中一行
                pagination: false,                  //分页
                rownumbers: false,                   //行号
                animate: true,                      //定义当节点展开或折叠时是否显示动画效果。
//                frozenColumns: [[
//	                { field: 'id', checkbox: true}  //和列的特性一样，但是这些列将被冻结在左边
//				]],
                columns: [[
                     { field: 'name', title: '分类名称', width: 80 }
                    , { field: 'code', title: '分类编码', width: 80 }
                    , { field: 'index', title: '排序', width: 80 }
                    , { field: 'description', title: '分类描述', width: 80 }
                ]]
            });

        });


        function submit() {

            $('#form').form('submit', {
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (msg) {
                    var msg1 = eval('(' + msg + ')');
                    if (msg1.IsSuccess) {
                        JSHelper.ShowInfo("保存成功", null, function () {
                            if ($("#id").val() != "") {
                                $('#win').dialog('close');
                            }
                            $('#grid').treegrid('reload');
                        });
                    }
                    else {
                        JSHelper.ShowError(msg1.Message);
                    }
                },
                error: function () {
                    JSHelper.ShowError(保存失败);
                }
            });
        }

        function add() {
            var row = $('#grid').treegrid('getSelected');
            if (row) {
                $('#win').dialog({
                    title: '新增分类',
                    iconCls: 'icon-add',
                    width: 400,
                    height: 244,
                    closed: false,
                    cache: false,
                    href: '/DicCategory/Add/' + row.id,
                    modal: true,
                    buttons: [{
                        text: '提交',
                        iconCls: 'icon-ok',
                        handler: submit
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#win').dialog('close');
                        }
                    }]                    
                });
            } else {
                JSHelper.ShowInfo("请选择要新增数据的上级分类");
                return;
            }
         }

         function edit() {
             var row = $('#grid').treegrid('getSelected');
             if (row) {
                 $('#win').dialog({
                     title: '修改分类',
                     iconCls: 'icon-save',
                     width: 400,
                     height: 244,
                     closed: false,
                     cache: false,
                     href: '/DicCategory/Edit/' + row.id,
                     modal: true,
                     buttons: [{
                         text: '提交',
                         iconCls: 'icon-ok',
                         handler: submit
                     }, {
                         text: '取消',
                         iconCls: 'icon-cancel',
                         handler: function () {
                             $('#win').dialog('close');
                         }
                     }]
                 });
             } else {
                 JSHelper.ShowInfo('请选择要修改的数据');
                 return;
             }
        }

        function del() {
            var rows = $('#grid').treegrid('getSelections');
            if (!rows || rows.length == 0) {
                JSHelper.ShowInfo("请选择要删除的数据");
                return;
            }
            var parm;
            $.each(rows, function (i, n) {
                if (i == 0) {
                    parm = "idList=" + n.id;
                }
                else {
                    parm += "&idList=" + n.id;
                }
            });           

            JSHelper.ShowConfirm("是否删除这些数据1?", null, function (r) {
                if (!r) {
                    return;
                }
                $.ajax({
                    type: "POST",
                    url: "/DicCategory/Delete/",
                    data: parm,
                    success: function (msg) {
                        if (msg.IsSuccess != null) {
                            if (msg.IsSuccess) {
                                JSHelper.ShowInfo('删除成功！', null, function () {
                                    $('#grid').treegrid("reload");
                                });
                            }
                            else {
                                JSHelper.ShowError(msg.Message);
                            }
                        }
                        else {
                            eval(msg);
                        }
                    },
                    error: function (msg) {
                        JSHelper.ShowError("删除失败！");
                    }
                });
            });
        }
    </script>
</head>
<body class="easyui-layout">
    <div region="center" style="padding: 5px;" border="false">        
        <table id="grid" toolbar="#toolbar">
        </table>
        <div id="toolbar" style="padding: 2px; height: auto">
            <a id="btnAdd" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-add" plain="true" onclick="add()">添加</a>
            <a id="btnUpdate" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-edit" plain="true" onclick="edit()">修改</a>
            <a id="btnDelete" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-remove" plain="true" onclick="del()">删除</a>
        </div>
    </div>
    <div id="win" />
</body>
</html>
